import modelExtend from 'dva-model-extend'
import queryString from 'query-string'
import * as basicData from 'services/basicData.js'
import { list, add, del, delall, get, edit } from 'services/exchangeset'
import { message } from 'antd'
import { pageModel } from './common'

export default modelExtend(pageModel, {
  namespace: 'exchangeset',
  state: {
    currenciesTypeData: [],
    currenciesCodeMapName: [],
  },
  subscriptions: {  
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/exchangeset') {
          const payload = queryString.parse(location.search) || { page: 1 }
          dispatch({
            type: 'list',
            payload,  
          })
          dispatch({
            type: 'initSelectData',
          })  
        }
      })
    },
  },

  effects: {
    * list ({ payload = {} }, { call, put }) {
      const data = yield call(list, payload)
      const listData = data.data
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
    },
    * delete ({ payload }, { call, put, select }) {
      const data = yield call(del, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.exchangeset)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'list' })
      } else {
        message.error('delete failed')
        throw data
      }
    },
    * update ({ payload }, { select, call, put }) {
      const id = yield select(({ exchangeset }) => exchangeset.currentItem.id)
      const newData = { ...payload, id }
      const data = yield call(edit, newData)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('update failed')
        throw data
      }
    },
    * initSelectData ({ payload }, { call, put }) {
      const currenciesData = yield call(basicData.listall, { type: 'Currencies' })
      let map = []
      if (currenciesData.success) {
        for (let i = 0; i < currenciesData.data.length; i++) {
          map[currenciesData.data[i].code] = currenciesData.data[i].name
        }
        yield put({
          type: 'updateState',
          payload: {
            currenciesTypeData: currenciesData.data,
            currenciesCodeMapName: map,
          },
        })
      }
    },
    * multiDelete ({ payload }, { call, put }) {
      const data = yield call(delall, payload)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        yield put({ type: 'list' })
      } else {
        message.error('delete failed')
        throw data
      }
    },
    * create ({ payload }, { call, put }) {
      payload = {
        id: 1,
        ...payload,
      }
      const data = yield call(add, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('create failed')
        throw data
      }
    },
    * showModal ({ payload }, { call, put }) {
      let sendData = {}
      sendData.id = payload.currentItem.id
      const data = yield call(get, sendData)
      if (data.success) {
        yield put({
          type: 'updateModalState',
          payload: {
            modalType: 'update',
            currentItem: data.data,
            modalVisible: true,
          },
        })
      } else {
        throw data
      }
    },
  },

  reducers: {

  },
})
